﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using WL_Store.Model;

namespace WL_Store.DAL
{
   public  class OrderFormDAL
    {
        /// <summary>
        /// 查询所有订单信息
        /// </summary>
        /// <returns></returns>
        public List<OrderFormModel> GetOrderForms(OrderFormSearchModel searchModel)
        {
            string sql = @"select o.orderID,o.comName,o.totalPrice,o.categoryId,g.category,o.amount,o.orderTime,o.IsSendout,o.IsDelete 
                               from OrderForm as o inner join GoodsType as g on o.categoryId=g.categoryId";

            //构造where的泛型集合
            List<string> listWhere = new List<string>();
            //构造参数化查询数组的泛型集合
            List<SqlParameter> listParameters = new List<SqlParameter>();
            //判断多条件查询的条件订单编号是否输入
            if (!string.IsNullOrWhiteSpace(searchModel.orderID))
            {
                listWhere.Add("o.orderID=@orderID");
                listParameters.Add(new SqlParameter("@orderID", searchModel.orderID));
            }
           
            if (searchModel.StartOrderTime.HasValue)
            {
                listWhere.Add("o.orderTime>=@StartOrderTime");
                listParameters.Add(new SqlParameter("@StartOrderTime", searchModel.StartOrderTime));
            }
            if (searchModel.EndOrderTime.HasValue)
            {
                listWhere.Add("o.orderTime<=@EndOrderTime");
                listParameters.Add(new SqlParameter("@EndAddtime", searchModel.EndOrderTime));
            }
            //判断多条件查询的条件商品类别是否输入
            if (searchModel.categoryId > 0)
            {
                listWhere.Add("o.categoryId=@categoryId");
                listParameters.Add(new SqlParameter("@categoryId", searchModel.categoryId));
            }

                //判断检索管理员信息是否为空
                if (listWhere.Count > 0)
            {
                sql += " where " + string.Join(" and ", listWhere.ToArray());
            }


            SqlDataReader readers = Dbhelper.ExecuteReader(sql, listParameters.ToArray());
            //构造出一个泛型集合
            List<OrderFormModel> list = new List<OrderFormModel>();
            //判断游标中是否有数据行
            if (readers.HasRows)
            {
                while (readers.Read())
                {
                    OrderFormModel model = new OrderFormModel();
                    model.orderID = readers["orderID"].ToString();
                    model.comName = readers["comName"].ToString();
                    model.totalPrice = Convert.ToDouble(readers["totalPrice"]);
                    model.categoryId = Convert.ToInt32(readers["categoryId"]);
                    model.amount = Convert.ToInt32(readers["amount"]);
                    model.orderTime = Convert.ToDateTime(readers["orderTime"]);
                    model.IsSendout = Convert.ToBoolean(readers["IsSendout"]);
                    model.IsDelete = Convert.ToBoolean(readers["IsDelete"]);
                    //追加到集合中
                    list.Add(model);
                }
                //关闭游标
                readers.Close();
            }
            return list;


        }

        /// <summary>
        /// 更改商品发货信息 返回true：已发货；返回false：未发货 
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public bool UpdateIsSendout(string orderID)
        {
            string sql = @"update OrderForm set IsSendout=1 where orderID=@orderID";
            SqlParameter[] parameters = {
            new SqlParameter("@orderID",orderID)
            };
            return Dbhelper.ExecuteNonQuery(sql, parameters) > 0;
        }
        /// <summary>
        /// 删除订单信息 返回true：删除成功；返回false：删除失败 
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public bool DeleteisDelete(string orderID)
        {
            string sql = @"update OrderForm set isDelete=1 where orderID=@orderID";
            SqlParameter[] parameters = {
            new SqlParameter("@orderID",orderID)
            };
            return Dbhelper.ExecuteNonQuery(sql, parameters) > 0;
        }


    }
}
